/*
 * Copyright 2014 Soichiro Kashima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tsingteng.cosfun.ui.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;

import com.nineoldandroids.view.ViewHelper;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.widget.ObservableListView;
import com.tsingteng.cosfun.widget.flexible.ScrollUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author :zhikuan
 * Date:18-8-2
 */
public class PersonTabListViewFragment extends PersonTabBaseFragment<ObservableListView> {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person_baselsit_layout, container, false);

        final ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
        final int flexibleSpaceImageHeight = getResources().getDimensionPixelSize(R.dimen.dp_260);

        View paddingView = new View(getContext());
        final int height = getResources().getDimensionPixelSize(R.dimen.dp_260);
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        paddingView.setLayoutParams(layoutParams);
        paddingView.setClickable(false);
        listView.addHeaderView(paddingView);
        listView.setPullLoadEnable(true);
        listView.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, getdata(20)));
        listView.setTouchInterceptionViewGroup((ViewGroup) view.findViewById(R.id.fragment_root));

        Bundle args = getArguments();
        if (args != null && args.containsKey(ARG_SCROLL_Y)) {
            final int scrollY = args.getInt(ARG_SCROLL_Y, 0);
            ScrollUtils.addOnGlobalLayoutListener(listView, new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
                    int offset = scrollY % flexibleSpaceImageHeight;
                    listView.setSelectionFromTop(0, -offset);
                }
            });
            updateFlexibleSpace(scrollY, view);
        } else {
            updateFlexibleSpace(0, view);
        }

        listView.setScrollViewCallbacks(this);

        updateFlexibleSpace(0, view);

        return view;
    }

    private ArrayList<String> getdata(int num) {
        ArrayList<String> items = new ArrayList<>();
        for (int i = 0; i <= num; i++) {
            items.add("item    " + i);
        }
        return items;
    }

    @SuppressWarnings("NewApi")
    @Override
    public void setScrollY(int scrollY, int threshold) {
        View view = getView();
        if (view == null) {
            return;
        }
        ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
        if (listView == null) {
            return;
        }
        View firstVisibleChild = listView.getChildAt(0);
        if (firstVisibleChild != null) {
            int offset = scrollY;
            int position = 0;
            if (threshold < scrollY) {
                int baseHeight = firstVisibleChild.getHeight();
                position = scrollY / baseHeight;
                offset = scrollY % baseHeight;
            }
            listView.setSelectionFromTop(position, -offset);
        }
    }

    @Override
    protected void updateFlexibleSpace(int scrollY, View view) {
        int flexibleSpaceImageHeight = getResources().getDimensionPixelSize(R.dimen.dp_260);

        View listBackgroundView = view.findViewById(R.id.list_background);
        ViewHelper.setTranslationY(listBackgroundView, Math.max(0, -scrollY + flexibleSpaceImageHeight));


//        FrameLayout layout = view.findViewById(R.id.fragment_root);
//        ViewHelper.setTranslationY(layout, Math.max(0, -scrollY + flexibleSpaceImageHeight));

        List<android.support.v4.app.Fragment> listFragment = getActivity().getSupportFragmentManager().getFragments();
        for (int i = 0; i < listFragment.size(); i++) {
            android.support.v4.app.Fragment fragment = listFragment.get(i);
            if (fragment instanceof HomeMineFragment) {
                ((HomeMineFragment) fragment).onScrollChanged(scrollY, (ObservableListView) view.findViewById(R.id.scroll));

            }
        }
       /* HomeMineFragment parent =  (HomeMineFragment)PersonTabListViewFragment.this. getParentFragment();
        if (parent != null) {
            parent.onScrollChanged(scrollY, (ObservableListView) view.findViewById(R.id.scroll));
        }*/
    }
}
